
package com.hlkj.pay.controller.pay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.pay.IPayChannelAppService;
import com.hlkj.pay.app.transfer.PayChannelDoToResp;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.pay.PayChannelQueryDto;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.vo.common.req.DeleteCodeReq;
import com.hlkj.pay.vo.merchant.req.UpdateStatusReq;
import com.hlkj.pay.vo.pay.req.PayChannelPageQueryReq;
import com.hlkj.pay.vo.pay.req.PayChannelReq;
import com.hlkj.pay.vo.pay.resp.PayChannelListResp;
import com.hlkj.pay.vo.pay.resp.PayChannelResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/09/23 11:33
 */
@Tag(name = "支付渠道配置")
@RestController
@RequestMapping("/v1/pay/channel")
@Slf4j
@RequiredArgsConstructor
public class PayChannelController extends BaseController {

    private final IPayChannelService payChannelService;

    private final IPayChannelAppService payChannelAppService;

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    CommonResult<PageResult<PayChannelListResp>> queryPayWayPage(@Validated PayChannelPageQueryReq pageParam) {
        PayChannelQueryDto payWayQueryDO = BeanUtil.copy(pageParam, PayChannelQueryDto.class);
        payWayQueryDO.setLikeCode(pageParam.getCode());
        payWayQueryDO.setLikeName(pageParam.getName());
        payWayQueryDO.setCode(null);
        payWayQueryDO.setName(null);
        PageResult<PayChannelDO> page = payChannelService.queryPage(payWayQueryDO);
        List<PayChannelDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, PayChannelDoToResp.INSTANCE.payChannelListRespList(list));
    }

    @Operation(summary = "不分页查询")
    @GetMapping("")
    CommonResult<List<PayChannelListResp>> queryPayChannelList(@Validated PayChannelPageQueryReq pageParam) {
        PayChannelQueryDto payWayQueryDO = BeanUtil.copy(pageParam, PayChannelQueryDto.class);
        payWayQueryDO.setLikeCode(pageParam.getCode());
        payWayQueryDO.setLikeName(pageParam.getName());
        payWayQueryDO.setCode(null);
        payWayQueryDO.setName(null);
        List<PayChannelDO> payWayList = payChannelService.queryList(payWayQueryDO);
        if (CollectionUtils.isEmpty(payWayList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(PayChannelDoToResp.INSTANCE.payChannelListRespList(payWayList));
    }

    @Operation(summary = "渠道详情")
    @GetMapping("/detail")
    CommonResult<PayChannelResp> detail(@RequestParam("code") String code) {
        PayChannelDO payChannelDO = payChannelService.queryByCode(code);
        if (payChannelDO == null) {
            return CommonResult.success(null);
        }
        PayChannelResp payChannelResp = PayChannelDoToResp.INSTANCE.payChannelResp(payChannelDO);
        if (StringUtils.hasText(payChannelResp.getChannelConfig())) {
            try {
                JsonUtils.parseArray(payChannelResp.getChannelConfig(), Object.class);
            }
            catch (Exception e) {
                log.info("服务商配置 参数解析失败 返回默认数组 code:｛}", code);
                payChannelResp.setChannelConfig(JsonUtils.toJsonString(new ArrayList<>()));
            }
        }
        else {
            payChannelResp.setChannelConfig(JsonUtils.toJsonString(new ArrayList<>()));
        }
        if (StringUtils.hasText(payChannelResp.getMerchantConfig())) {
            try {
                JsonUtils.parseArray(payChannelResp.getMerchantConfig(), Object.class);
            }
            catch (Exception e) {
                log.info("普通商户配置 参数解析失败 返回默认数组 code:｛}", code);
                payChannelResp.setMerchantConfig(JsonUtils.toJsonString(new ArrayList<>()));
            }
        }
        else {
            payChannelResp.setMerchantConfig(JsonUtils.toJsonString(new ArrayList<>()));
        }
        if (StringUtils.hasText(payChannelResp.getSpecialMerchantConfig())) {
            try {
                JsonUtils.parseArray(payChannelResp.getSpecialMerchantConfig(), Object.class);
            }
            catch (Exception e) {
                log.info("特约商户配置 参数解析失败 返回默认数组 code:｛}", code);
                payChannelResp.setSpecialMerchantConfig(JsonUtils.toJsonString(new ArrayList<>()));
            }
        }
        else {
            payChannelResp.setSpecialMerchantConfig(JsonUtils.toJsonString(new ArrayList<>()));
        }

        return CommonResult.success(payChannelResp);
    }

    @Operation(summary = "新增支付渠道")
    @PostMapping("")
    CommonResult<Void> savePayChannel(@Validated @RequestBody PayChannelReq payWayReq) {
        return payChannelAppService.savePayChannel(payWayReq);
    }

    @Operation(summary = "更新支付渠道")
    @PutMapping("")
    CommonResult<Void> updatePayChannel(@Validated @RequestBody PayChannelReq payWayReq) {
        return payChannelAppService.updatePayChannel(payWayReq);
    }

    @Operation(summary = "删除支付渠道")
    @DeleteMapping("")
    CommonResult<Void> delPayChannel(@Validated @RequestBody DeleteCodeReq deleteCodeReq) {
        return payChannelAppService.delPayChannel(deleteCodeReq.getCode());
    }

    @Operation(summary = "更新渠道状态")
    @PostMapping("/status")
    CommonResult<Void> updatePayChannelStatus(@Validated @RequestBody UpdateStatusReq commonUpdateStatusReq) {
        return payChannelAppService.updatePayChannelStatus(commonUpdateStatusReq);
    }
}
